package us.nchu.tomtiger.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import us.nchu.tomtiger.utils.EncryptUtil;
import us.nchu.tomtiger.utils.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * Created by XiongXuan on 2018/1/2.
 */
@Component
public class RegisterInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        // 判断访问方式的是get还是post
        if (httpServletRequest.getMethod().equals("GET")) {
            return preGet(httpServletRequest, httpServletResponse);
        }
        else
            return prePost(httpServletRequest, httpServletResponse);
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

    // 如果访问的方式是get，则表明用户是在打开这个网页
    private boolean preGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
        // 判断用户是否已经登录，如果已经登录，则跳转到个人中心界面
        if (UserUtil.isLogin(httpServletRequest)) {
            httpServletResponse.sendRedirect("/center");
            return false;
        }
        return true;
    }

    // 如果访问的方式是post，则表明是在注册，这里判断请求的参数是否正确
    private boolean prePost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws UnsupportedEncodingException {
        // 获取传入的参数
        String phoneNumber = httpServletRequest.getParameter("phoneNumber");
        String verifyCode = httpServletRequest.getParameter("verifyCode");
        String passwd = httpServletRequest.getParameter("password");
        String nickName = new String(httpServletRequest.getParameter("nickName").getBytes("UTF-8"));
        String timestamp = httpServletRequest.getParameter("timestamp");
        String sha1_result = httpServletRequest.getParameter("sha1_result");

        // 如果传入的参数有一个为空，则不继续进行操作
        if (phoneNumber == null || verifyCode == null || passwd == null || timestamp == null || sha1_result == null) {
            return false;
        }

        // 检查timestamp，如果是超时的请求，直接丢弃
        if (!UserUtil.checkTime(timestamp)){
            return false;
        }

        // 一个按key值降序排序的map
        Map<String, String> map = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj1.compareTo(obj2);
                    }
                });

        map.put("phoneNumber", phoneNumber);
        map.put("verifyCode", verifyCode);
        map.put("passwd", passwd);
        map.put("nickName", nickName);
        map.put("timestamp", timestamp);

        // 把所有参数的值，按顺序拼接
        StringBuilder stringBuffer = new StringBuilder();
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            stringBuffer.append(map.get(key));
        }

        // 算出拼接后字符串的sha1值，和传来的sha1值对比，不同就拦截，相同就放行
        String check = EncryptUtil.getSha1(stringBuffer.toString());
        return check.equals(sha1_result);
    }


}
